/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bytedance.scene.ui.bottomnavigation;

import ohos.agp.render.Canvas;
import ohos.agp.render.Texture;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.media.image.common.AlphaType;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;

import java.util.Optional;

/**
 * BottomNavigationItem
 *
 * @since 2021-06-16
 */
public class BottomNavigationItem {
    private PixelMap orgPixelMap;
    private PixelMap orgInActivePixelMap;

    private PixelMap activePixelMap;
    private PixelMap inActivePixelMap;
    private PixelMap backgroundPixelMap;

    private int iconSize;

    private int mIconResource;

    private int mInactiveIconResource;
    private boolean isInActiveIconAvailable = false;

    private String mTitle;

    private int mActiveColor;

    private int mInActiveColor;
    private int mBackgrounColor;

    private BadgeItem badgeItem;

    private BottomNavigationBar bottomNavigationBar;

    /**
     * BottomNavigationItem
     *
     * @param iconResource iconResource
     * @param title title
     * @param context context
     */
    public BottomNavigationItem(int iconResource, String title, Context context) {
        this.mIconResource = iconResource;
        this.mTitle = title;
        Optional<PixelMap> optional = ResUtil.getPixelMap(context, mIconResource);
        orgPixelMap = optional.isPresent() ? optional.get() : null;
    }

    /**
     * BottomNavigationItem
     *
     * @param pixelMap pixelMap
     * @param title title
     */
    public BottomNavigationItem(PixelMap pixelMap, String title) {
        this.orgPixelMap = pixelMap;
        this.mTitle = title;
    }

    /**
     * setInactiveIconResource
     *
     * @param inactiveIconResource inactiveIconResource
     * @param context context
     * @return BottomNavigationItem
     */
    public BottomNavigationItem setInactiveIconResource(int inactiveIconResource, Context context) {
        this.mInactiveIconResource = inactiveIconResource;
        Optional<PixelMap> optional = ResUtil.getPixelMap(context, mInactiveIconResource);
        orgInActivePixelMap = optional.isPresent() ? optional.get() : null;
        isInActiveIconAvailable = true;
        return this;
    }

    /**
     * setActiveColor
     *
     * @param color color
     * @return BottomNavigationItem
     */
    public BottomNavigationItem setActiveColor(int color) {
        this.mActiveColor = color;
        return this;
    }

    /**
     * setInActiveColor
     *
     * @param color color
     * @return BottomNavigationItem
     */
    public BottomNavigationItem setInActiveColor(int color) {
        this.mInActiveColor = color;
        return this;
    }

    /**
     * 返回标题
     *
     * @return 标题
     */
    public String getTitle() {
        return this.mTitle;
    }

    PixelMap getInactiveIcon() {
        return orgPixelMap;
    }

    boolean isInActiveIconAvailable() {
        return isInActiveIconAvailable;
    }

    int getActiveColor() {
        return mActiveColor;
    }

    int getInActiveColor() {
        return mInActiveColor;
    }

    int getBackgroundColor() {
        return mBackgrounColor;
    }

    /**
     * setBadgeItem
     *
     * @param badgeItem badgeItem
     * @return BottomNavigationItem
     */
    public BottomNavigationItem setBadgeItem(BadgeItem badgeItem) {
        badgeItem.setBottomNavigationBar(this.bottomNavigationBar);
        this.badgeItem = badgeItem;
        return this;
    }

    public BadgeItem getBadgeItem() {
        return badgeItem;
    }

    void setupPixelMap(BottomNavigationBar bottomNavigationBar1, int iconSize1) {
        this.bottomNavigationBar = bottomNavigationBar1;
        if (badgeItem != null) {
            badgeItem.setBottomNavigationBar(bottomNavigationBar1);
        }
        this.iconSize = iconSize1;
        this.mBackgrounColor = bottomNavigationBar1.getBackgroundColor();
        if (orgPixelMap != null) {
            generateIconBitmaps(bottomNavigationBar1);
        }
    }

    PixelMap getActivePixelMap() {
        return activePixelMap;
    }

    PixelMap getInActivePixelMap() {
        return inActivePixelMap;
    }

    PixelMap getBackgroundPixelMap() {
        return backgroundPixelMap;
    }

    private PixelMap copy(PixelMap source, int width, int height) {
        PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
        initializationOptions.pixelFormat = PixelFormat.ARGB_8888;
        initializationOptions.alphaType = AlphaType.PREMUL;
        initializationOptions.size = new Size(width, height);
        return PixelMap.create(source, initializationOptions);
    }

    private PixelMap scaleIcon(PixelMap origin) {
        int width = origin.getImageInfo().size.width;
        int height = origin.getImageInfo().size.height;
        int size = Math.max(width, height);
        if (size == iconSize) {
            return origin;
        } else if (size > iconSize) {
            int scaledWidth;
            int scaledHeight;
            if (width > iconSize) {
                scaledWidth = iconSize;
                scaledHeight = (int) (iconSize * ((float) height / width));
            } else {
                scaledHeight = iconSize;
                scaledWidth = (int) (iconSize * ((float) width / height));
            }
            return copy(origin, scaledWidth, scaledHeight);
        } else {
            return origin;
        }
    }

    private void generateIconBitmaps(BottomNavigationBar bottomNavigationBar1) {
        if (orgPixelMap == null) {
            return;
        }
        orgPixelMap = scaleIcon(orgPixelMap);
        if (inActivePixelMap != null) {
            inActivePixelMap = scaleIcon(inActivePixelMap);
        }
        PixelMap origin = orgPixelMap;
        activePixelMap = copy(origin, origin.getImageInfo().size.width, origin.getImageInfo().size.height);
        Canvas canvas = new Canvas(new Texture(activePixelMap));
        canvas.drawColor(
                mActiveColor != 0 ? mActiveColor : bottomNavigationBar1.getActiveColor(), Canvas.PorterDuffMode.SRC_IN);
        if (isInActiveIconAvailable && orgInActivePixelMap != null) {
            inActivePixelMap =
                    copy(
                            inActivePixelMap,
                            inActivePixelMap.getImageInfo().size.width,
                            inActivePixelMap.getImageInfo().size.height);
        } else {
            inActivePixelMap = copy(origin, origin.getImageInfo().size.width, origin.getImageInfo().size.height);
            canvas = new Canvas(new Texture(inActivePixelMap));
            canvas.drawColor(
                    mInActiveColor != 0 ? mInActiveColor : bottomNavigationBar1.getInActiveColor(),
                    Canvas.PorterDuffMode.SRC_IN);
        }
        backgroundPixelMap = copy(origin, origin.getImageInfo().size.width, origin.getImageInfo().size.height);
        canvas = new Canvas(new Texture(backgroundPixelMap));
        canvas.drawColor(mBackgrounColor, Canvas.PorterDuffMode.SRC_IN);
    }
}
